home *** CD-ROM | disk | FTP | other *** search
- #define BASE_RES_ID 400
- #define ABOUT_ALERT 401
- #define BAD_SYS_ALERT 402
- #define NIL_POINTER 0L
- #define MOVE_TO_FRONT -1L
- #define REMOVE_ALL_EVENTS 0
-
- #define MIN_SLEEP 0L
- #define NIL_MOUSE_REGION 0L
-
- #define DRAG_THRESHOLD 30
-
- #define SAVE_BUTTON 1
- #define CANCEL_BUTTON 2
- #define TIME_FIELD 4
- #define S_OR_M_FIELD 5
- #define SOUND_ON_BOX 6
- #define ICON_ON_BOX 7
- #define ALERT_ON_BOX 8
- #define SECS_RADIO 10
- #define MINS_RADIO 11
-
- #define DEFAULT_SECS_ID 401
- #define DEFAULT_MINS_ID 402
-
- #define ON 1
- #define OFF 0
-
- #define SECONDS 0
- #define MINUTES 1
- #define SECONDS_PER_MINUTE 60
-
- #define TOP 25
- #define LEFT 12
-
- #define MARK_APPLICATION 1
-
- #define APPLE_MENU_ID BASE_RES_ID
- #define FILE_MENU_ID BASE_RES_ID+1
- #define ABOUT_ITEM_ID 1
-
- #define CHANGE_ITEM 1
- #define START_STOP_ITEM 2
- #define KILL_ITEM 3
- #define QUIT_ITEM 4
-
- #define SYS_VERSION 1
-
- DialogPtr gSettingsDialog;
- Rect gDragRect;
- Boolean gDone, gCounting, gNotify_set;
- char gSeconds_or_minutes = SECONDS;
- StringHandle gNotifyStrH, gDefaultSecsH, gDefaultMinsH;
- NMRec gMyNMRec;
- MenuHandle gAppleMenu, gFileMenu;
- EventRecord gTheEvent;
-
- struct
- {
- Str255 timeString;
- int sound;
- int icon;
- int alert;
- int secsRadio;
- int minsRadio;
- } savedSettings;
-
-
- /**************** main **************************/
-
- main()
- {
- ToolBoxInit();
- if (Sys6OrLater())
- {
- DialogInit();
- MenuBarInit();
- SetUpDragRect();
- NotifyInit();
- MainLoop();
- }
- }
-
-
- /********************* ToolBoxInit ******************/
-
- ToolBoxInit()
- {
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent, REMOVE_ALL_EVENTS);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NIL_POINTER);
- InitCursor();
- }
-
-
- /********************* Sys6OrLater ******************/
-
- int Sys6OrLater()
- {
- OSErr status;
- SysEnvRec SysEnvData;
-
- status = SysEnvirons(SYS_VERSION, &SysEnvData);
- if ((status != noErr)||(SysEnvData.systemVersion < 0x0600))
- {
- StopAlert(BAD_SYS_ALERT, NIL_POINTER);
- return(FALSE);
- }
- else
- return(TRUE);
- }
-
- /******************** DialogInit **********************/
-
- DialogInit()
- {
- int itemType;
- Rect itemRect;
- Handle itemHandle;
-
- gDefaultSecsH = GetString (DEFAULT_SECS_ID);
- gDefaultMinsH = GetString (DEFAULT_MINS_ID);
-
- gSettingsDialog = GetNewDialog (BASE_RES_ID, NIL_POINTER,
- MOVE_TO_FRONT);
- GetDItem (gSettingsDialog, SECS_RADIO, &itemType, &itemHandle,
- &itemRect);
- SetCtlValue (itemHandle, ON);
- GetDItem (gSettingsDialog, SOUND_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- SetCtlValue (itemHandle, ON);
- GetDItem (gSettingsDialog, ICON_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- SetCtlValue (itemHandle, ON);
- GetDItem (gSettingsDialog, ALERT_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- SetCtlValue (itemHandle, ON);
- }
-
- /************************ MenuBarInit ********************/
-
- MenuBarInit()
- {
- Handle myMenuBar;
-
- myMenuBar = GetNewMBar (BASE_RES_ID);
- SetMenuBar (myMenuBar);
- gAppleMenu = GetMHandle (APPLE_MENU_ID);
- AddResMenu (gAppleMenu, 'DRVR');
- gFileMenu = GetMHandle (FILE_MENU_ID);
- DrawMenuBar();
- }
-
- /******************** SetUpDragRect *******************/
-
- SetUpDragRect()
- {
- gDragRect = screenBits.bounds;
- gDragRect.left += DRAG_THRESHOLD;
- gDragRect.right -= DRAG_THRESHOLD;
- gDragRect.bottom -= DRAG_THRESHOLD;
- }
-
- /******************** NotifyInit ***********************/
-
- NotifyInit()
- {
- gNotifyStrH = GetString (BASE_RES_ID);
- gMyNMRec.qType = nmType;
- gMyNMRec.nmMark = MARK_APPLICATION;
- gMyNMRec.nmResp = NIL_POINTER;
- }
-
- /******************** MainLoop ***************************/
-
- MainLoop()
- {
- gDone = FALSE;
- gCounting = FALSE;
- gNotify_set = FALSE;
-
- while (gDone == FALSE)
- {
- HandleEvent();
- }
- }
-
- /********************** HandleEvent ************************/
-
- HandleEvent ()
- {
- char theChar;
-
- WaitNextEvent (everyEvent, &gTheEvent, MIN_SLEEP,
- NIL_MOUSE_REGION);
-
- switch (gTheEvent.what)
- {
- case mouseDown:
- HandleMouseDown();
- break;
- case keyDown:
- case autoKey:
- theChar = gTheEvent.message & charCodeMask;
- if (( gTheEvent.modifiers & cmdKey) != 0)
- HandleMenuChoice(MenuKey(theChar));
- break;
- }
- }
-
- /************************ HandleMouse ******************/
-
- HandleMouseDown()
- {
- WindowPtr whichWindow;
- short int thePart;
- long int menuChoice, windSize;
-
- thePart = FindWindow (gTheEvent.where, &whichWindow);
- switch (thePart)
- {
- case inMenuBar:
- menuChoice = MenuSelect (gTheEvent.where);
- HandleMenuChoice(menuChoice);
- break;
- case inSysWindow:
- SystemClick (&gTheEvent, whichWindow);
- break;
- case inDrag:
- DragWindow(whichWindow,gTheEvent.where, &gDragRect);
- break;
- case inGoAway:
- gDone = TRUE;
- break;
- }
- }
-
- /***************** HandleMenuChoice ***************/
-
- HandleMenuChoice(menuChoice)
- long int menuChoice;
- {
- int theMenu;
- int theItem;
-
- if (menuChoice !=0)
- {
- theMenu = HiWord (menuChoice);
- theItem = LoWord (menuChoice);
- switch(theMenu)
- {
- case APPLE_MENU_ID:
- HandleAppleChoice(theItem);
- break;
- case FILE_MENU_ID:
- HandleFileChoice(theItem);
- break;
- }
- }
- HiliteMenu(0);
- }
-
- /********************* HandleAppleChoice *************/
-
- HandleAppleChoice(theItem)
- int theItem;
- {
- Str255 accName;
- int accNumber;
- short int itemNumber;
-
- switch (theItem)
- {
- case ABOUT_ITEM_ID:
- NoteAlert(ABOUT_ALERT, NIL_POINTER);
- break;
- default:
- GetItem(gAppleMenu,theItem,accName);
- accNumber = OpenDeskAcc (accName);
- break;
- }
- }
-
- /**************** HandleFileChoice ***********************/
-
- HandleFileChoice (theItem)
- int theItem;
- {
- Str255 timeString;
- long countDownTime;
- int itemType;
- Rect itemRect;
- Handle itemHandle;
-
- switch (theItem)
- {
- case CHANGE_ITEM:
- HandleDialog();
- break;
- case START_STOP_ITEM:
- if (gCounting)
- {
- SetItem (gFileMenu, theItem,"\pStart Countdown");
- gCounting = FALSE;
- } else
- {
- HiliteMenu(0);
- GetDItem (gSettingsDialog, TIME_FIELD,
- &itemType, &itemHandle, &itemRect);
- GetIText (itemHandle, &timeString);
- StringToNum (timeString, &countDownTime);
-
- DisableItem (gFileMenu, CHANGE_ITEM);
- SetItem (gFileMenu, theItem, "\pStop Countdown");
- CountDown (countDownTime);
- EnableItem (gFileMenu, CHANGE_ITEM);
- SetItem (gFileMenu, theItem, "\pStart Countdown");
- }
- break;
- case KILL_ITEM:
- NMRemove (&gMyNMRec);
- HUnlock (gNotifyStrH);
- DisableItem (gFileMenu, KILL_ITEM);
- gNotify_set = FALSE;
- break;
- case QUIT_ITEM:
- gCounting = FALSE;
- gDone = TRUE;
- break;
- }
- }
-
- /****************** HandleDialog **************************/
-
- HandleDialog()
- {
- int itemHit, dialogDone = FALSE;
- long alarmDelay;
- Str255 delayString;
- int itemType;
- Rect itemRect;
- Handle itemHandle;
-
- ShowWindow (gSettingsDialog);
- SaveSettings();
-
- while (dialogDone == FALSE)
- {
- ModalDialog (NIL_POINTER, &itemHit);
- switch (itemHit)
- {
- case SAVE_BUTTON:
- HideWindow (gSettingsDialog);
- dialogDone = TRUE;
- break;
- case CANCEL_BUTTON:
- HideWindow (gSettingsDialog);
- RestoreSettings();
- dialogDone = TRUE;
- break;
- case SOUND_ON_BOX:
- GetDItem (gSettingsDialog, SOUND_ON_BOX,
- &itemType, &itemHandle, &itemRect);
- SetCtlValue (itemHandle, !GetCtlValue(itemHandle));
- break;
- case ICON_ON_BOX:
- GetDItem (gSettingsDialog, ICON_ON_BOX,
- &itemType, &itemHandle, &itemRect);
- SetCtlValue (itemHandle, !GetCtlValue(itemHandle));
- break;
- case ALERT_ON_BOX:
- GetDItem (gSettingsDialog, ALERT_ON_BOX,
- &itemType, &itemHandle, &itemRect);
- SetCtlValue (itemHandle, !GetCtlValue(itemHandle));
- break;
- case SECS_RADIO:
- gSeconds_or_minutes = SECONDS;
- GetDItem (gSettingsDialog, MINS_RADIO,
- &itemType, &itemHandle, &itemRect);
- SetCtlValue (itemHandle, OFF);
- GetDItem (gSettingsDialog, SECS_RADIO,
- &itemType, &itemHandle, &itemRect);
- SetCtlValue (itemHandle, ON);
- GetDItem (gSettingsDialog, S_OR_M_FIELD,
- &itemType, &itemHandle, &itemRect);
- SetIText (itemHandle, "\pseconds");
- GetDItem (gSettingsDialog, TIME_FIELD,
- &itemType, &itemHandle, &itemRect);
- HLock (gDefaultSecsH);
- SetIText (itemHandle, *gDefaultSecsH);
- HUnlock (gDefaultSecsH);
- break;
- case MINS_RADIO:
- gSeconds_or_minutes = MINUTES;
- GetDItem (gSettingsDialog, SECS_RADIO,
- &itemType, &itemHandle, &itemRect);
- SetCtlValue (itemHandle, OFF);
- GetDItem (gSettingsDialog, MINS_RADIO,
- &itemType, &itemHandle, &itemRect);
- SetCtlValue (itemHandle, ON);
- GetDItem (gSettingsDialog, S_OR_M_FIELD,
- &itemType, &itemHandle, &itemRect);
- SetIText (itemHandle, "\pminutes");
- GetDItem (gSettingsDialog, TIME_FIELD,
- &itemType, &itemHandle, &itemRect);
- HLock (gDefaultMinsH);
- SetIText (itemHandle, *gDefaultMinsH);
- HUnlock (gDefaultMinsH);
- break;
- }
- }
- }
-
- /************************ Save Setting ********************/
-
- SaveSettings()
- {
- int itemType;
- Rect itemRect;
- Handle itemHandle;
-
- GetDItem (gSettingsDialog, TIME_FIELD, &itemType, &itemHandle,
- &itemRect);
- GetIText (itemHandle, &(savedSettings.timeString));
- GetDItem (gSettingsDialog, SOUND_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- savedSettings.sound = GetCtlValue (itemHandle);
- GetDItem (gSettingsDialog, ICON_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- savedSettings.icon = GetCtlValue (itemHandle);
- GetDItem (gSettingsDialog, ALERT_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- savedSettings.alert = GetCtlValue (itemHandle);
- GetDItem (gSettingsDialog, SECS_RADIO, &itemType, &itemHandle,
- &itemRect);
- savedSettings.secsRadio = GetCtlValue (itemHandle);
- GetDItem (gSettingsDialog, MINS_RADIO, &itemType, &itemHandle,
- &itemRect);
- savedSettings.minsRadio = GetCtlValue (itemHandle);
- }
-
- /************************* RestoreSettings ********************/
-
- RestoreSettings()
- {
- int itemType;
- Rect itemRect;
- Handle itemHandle;
-
- GetDItem (gSettingsDialog, TIME_FIELD, &itemType, &itemHandle,
- &itemRect);
- SetIText (itemHandle, savedSettings.timeString);
- GetDItem (gSettingsDialog, SOUND_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- SetCtlValue (itemHandle, savedSettings.sound);
- GetDItem (gSettingsDialog, ICON_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- SetCtlValue (itemHandle, savedSettings.icon);
- GetDItem (gSettingsDialog, ALERT_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- SetCtlValue (itemHandle, savedSettings.alert);
- GetDItem (gSettingsDialog, SECS_RADIO, &itemType, &itemHandle,
- &itemRect);
- SetCtlValue (itemHandle, savedSettings.secsRadio);
- GetDItem (gSettingsDialog, MINS_RADIO, &itemType, &itemHandle,
- &itemRect);
- SetCtlValue (itemHandle, savedSettings.minsRadio);
-
- if (savedSettings.secsRadio == ON)
- {
- GetDItem (gSettingsDialog, S_OR_M_FIELD, &itemType,
- &itemHandle, &itemRect);
- SetIText (itemHandle, "\pseconds");
- } else
- {
- GetDItem (gSettingsDialog, S_OR_M_FIELD, &itemType,
- &itemHandle, &itemRect);
- SetIText (itemHandle, "\pminutes");
- }
- }
-
- /************************** Countdown ****************************/
-
- CountDown (numSecs)
- long numSecs;
- {
- long myTime, oldTime, difTime;
- Str255 myTimeString;
- WindowPtr countDownWindow;
-
- countDownWindow = GetNewWindow (BASE_RES_ID, NIL_POINTER,
- MOVE_TO_FRONT);
- SetPort (countDownWindow);
- ShowWindow (countDownWindow);
- TextFace (bold);
- TextSize (24);
-
- GetDateTime (&myTime);
- oldTime = myTime;
- if (gSeconds_or_minutes == MINUTES)
- numSecs *= SECONDS_PER_MINUTE;
- gCounting = TRUE;
-
- while ((numSecs > 0) && (gCounting))
- {
- HandleEvent();
- if (gCounting)
- {
- MoveTo (LEFT, TOP);
- GetDateTime (&myTime);
- if (myTime != oldTime)
- {
- difTime = myTime - oldTime;
- numSecs = numSecs - difTime;
- oldTime = myTime;
- NumToString (numSecs, myTimeString);
- EraseRect ( &(countDownWindow->portRect));
- DrawString (myTimeString);
- }
- }
- }
- if (gCounting)
- SetNotification();
- gCounting = FALSE;
- HideWindow (countDownWindow);
- }
-
- /************************* SetNotification *******************/
-
- SetNotification()
- {
- int itemType;
- Rect itemRect;
- Handle itemHandle;
-
- if (gNotify_set)
- {
- NMRemove (&gMyNMRec);
- HUnlock (gNotifyStrH);
- }
-
- GetDItem (gSettingsDialog, ICON_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- if (GetCtlValue (itemHandle))
- gMyNMRec.nmSIcon = GetResource ('SICN', BASE_RES_ID);
- else
- gMyNMRec.nmSIcon = NIL_POINTER;
-
- GetDItem (gSettingsDialog, SOUND_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- if (GetCtlValue (itemHandle))
- gMyNMRec.nmSound = GetResource ('snd ', BASE_RES_ID);
- else
- gMyNMRec.nmSound = NIL_POINTER;
-
- GetDItem (gSettingsDialog, ALERT_ON_BOX, &itemType, &itemHandle,
- &itemRect);
- if (GetCtlValue(itemHandle))
- {
- MoveHHi (gNotifyStrH);
- HLock (gNotifyStrH);
- gMyNMRec.nmStr = *gNotifyStrH;
- }
- else
- gMyNMRec.nmStr = NIL_POINTER;
-
- NMInstall (&gMyNMRec);
- EnableItem (gFileMenu, KILL_ITEM);
- gNotify_set = TRUE;
- }
-
-
-
-
-
-
-
-
-